#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 加载图片
    QPixmap pixmap(":/img/images/img.jpeg");
    ui->label->setPixmap(pixmap);

    // 安装事件过滤器
    installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    // 检测鼠标单击事件
    if (event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        moveImage(mouseEvent->pos());
        return true;
    }
    return false;
}

void MainWindow::moveImage(const QPoint &clickPos)
{
    // 计算图片应该移动的位置
    int newX = clickPos.x() - ui->label->width() / 2;
    int newY = clickPos.y() - ui->label->height() / 2;
    ui->label->move(newX, newY);
}
